<?php
session_start ();
include_once ('../config.php');
include_once ('rennclient/RennClient.php');
$renn_client = new RennClient ( RENREN_APP_KEY, RENREN_APP_SECRET );
$renn_client->setDebug ( DEBUG_MODE );

$cate='chongwu';
$nameArr = array('东经109度23分琬', '诚心感恩感', '宇宙无敌龅牙葵福');
$c=count($nameArr)-1;
$i=rand(0,$c);
$name = $nameArr[$i];
$name ='张奇虎';
$contentArr = array('您好，喜欢宠物的朋友欢迎关注@萌乐宠官网 喵~。', '喵~喜欢宠物的朋友们欢迎关注@萌乐宠官网 嘻嘻', '您好，喜欢宠物吗？欢迎关注@萌乐宠官网 哦，各种宠物，各种卖萌', '喵一个，@萌乐宠官网 各种宠物萌图，欢迎关注。', '您如果喜欢宠物欢迎关注 @萌乐宠官网 各种好玩的宠物图片，非常萌哦~~~');
$c=count($contentArr)-1;
$i=rand(0,$c);
$content = $contentArr[$i];
$content = '您好，打扰了，我们在做接口测试，如果您收到这条信息，我们深表歉意，谢谢您的理解~';
/*ID有问题*/
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
mysql_select_db(DB_NAME,$con);
mysql_query("SET NAMES utf8");
$sql="select * from cai_uid where name='".$name."' and sns='renren'";
$result=mysql_query($sql);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$token = explode('_',$row['token']);
$MAC_TOKEN = $token[0];
$MAC_KEY = $token[1];
$MAC_ALGORITHM = $token[2];
$accessToken = new AccessToken ( TokenType::MAC, $MAC_TOKEN, null, $MAC_KEY, $MAC_ALGORITHM );
$renn_client->authWithToken($accessToken);
//dump($renn_client);

/*获取我的信息*/
$user_service = $renn_client->getUserService ();
$user = $user_service->getUserLogin ();
dump($user);

$user_id=suiji($cate);
echo $user_id;
/*获取状态列表*/
$status_service = $renn_client->getStatusService();
$user_status =$status_service->listStatus($user_id,1,1);

while(!isset($user_status[0]['id'])){
    $user_id=suiji($cate);
    $user_status =$status_service->listStatus($user_id,1,1);
}
dump($user_status);

/*添加评论*/
$comment_service = $renn_client->getCommentService();
$user_comment =$comment_service->putComment($content,0,'STATUS',$user_id,$user_status[0]['id']);
dump($user_comment);
if(isset($user_comment['content'])){
	$text = 'http://www.renren.com/'.$user_id;
    writelog($text,0);
    echo '成功';
}else{
    writelog('失败',1);
	echo '失败';
}
function writelog($str,$success)
{
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $sql="INSERT INTO `cai_log` (`id`, `success`, `content` , `data`,`from`,`sns`) VALUES (NULL,'".$success."','".$str."','".time()."','huifu','renren');";
    $result=mysql_query($sql);
    if($result){
        echo '记录数据成功<br/>';
    }else{
        echo '记录数据失败<br/>';
    }
}

function suiji($cate){
    $con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); 
    mysql_select_db(DB_NAME,$con);
    mysql_query("SET NAMES utf8");
    $sql="select * from cai_fensi where done='0' and cate='".$cate."' and sns = 'renren' ORDER BY rand() LIMIT 1"; 
    $result=mysql_query($sql);
    $row = mysql_fetch_array($result, MYSQL_ASSOC);
    $id=$row['id'];
    $user_id=$row['user_id'];
    $sql2="UPDATE `cai_fensi` SET `done` ='1' WHERE  id='".$id."';";
    $result2=mysql_query($sql2);
    if($result2){
        return $user_id;
    }else{
        return '0';
    }
    
}
?>
